﻿"use strict";
define(['admin/app', "service/admin/consumeItemService", "service/admin/settingService", "angular-kindeditor", "ui.file.manage"], function (app) {
    app.controller('consumeItemController', function ($scope, $stateParams, $location, $uibModal, $state, utils, consumeItemService, settingService) {
        $scope.query = {
            page: 1, size: 20, q: '', title: ''
        };
        $scope.editConfig = {
            autoHeightMode: true,
            items: ['source', 'preview', '|', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', 'image']
        };
        $.extend($scope.query, $stateParams);
        var methods = {
            textAreaChanged: function (wordCount) {
                if ($scope.model.detail) {
                    $scope.textcount = wordCount - $scope.model.detail.length;
                    if ($scope.textcount <= 0) {
                        $scope.textcount = 0;
                        $("#textcount").css("color", "red")
                    } else {
                        $("#textcount").css("color", "#ccc")
                    }
                    $("#textcount").text($scope.model.detail.length + "/" + $scope.textcount);

                    if ($scope.model.detail.length > wordCount) {
                        $scope.model.detail = $scope.model.detail.substr(0, wordCount);
                    }
                }
            },
            search: function () {
                consumeItemService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            get_token_setting: function () {
                settingService.getTokenSetting().success(function (response) {
                    $scope.token_setting = response.data;
                });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    consumeItemService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                } else {
                    consumeItemService.get({ id: 0 }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '新增';
                }
            },
            save: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                if (model.id == 0) {
                    consumeItemService.add(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                } else {
                    consumeItemService.update(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
            },
            updateDisabled: function (merchant_id, isDisabled) {
                var tip = isDisabled == 1 ? "停用" : "启用";
                var modal = utils.confirm({ msg: "是否需要" + tip + "该消费", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    consumeItemService.updateDisabled(merchant_id, isDisabled).success(function () {
                        methods.search();
                    });
                })
            },
            goBack: function () {
                history.back();
            },
            search_page: function (page) {
                $scope.query.page = page;
                methods.search('page', $scope.query.page);
            },
            getUserComsumeHiss: function (type) {
                if ($stateParams.type != undefined && $stateParams.type != null)
                    $scope.query.consume_type_value = $stateParams.type;
                if ($stateParams.productId != undefined && $stateParams.productId != null)
                    $scope.query.product_id = $stateParams.productId;
                consumeItemService.getUserComsumeHiss($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            userComsumeHis_page: function (page) {
                $scope.query.page = page;
                methods.getUserComsumeHiss('page', $scope.query.page);
            },


            getConsumeProducts: function () {
                consumeItemService.getConsumeProducts($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            editConsumeProduct: function () {
                if ($stateParams.id > 0) {
                    consumeItemService.getConsumeProduct({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                } else {
                    consumeItemService.getConsumeProduct({ id: 0 }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '新增';
                }
            },
            saveConsumeProduct: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                if (model.id == 0) {
                    consumeItemService.addConsumeProduct(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.consumeProduct_list();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                } else {
                    consumeItemService.updateConsumeProduct(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.consumeProduct_list();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
            },
            updateConsumeProductStatus: function (id, status) {
                var tip = status == 1 ? "上架" : "下架";
                var modal = utils.confirm({ msg: "是否需要" + tip + "该商品", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    consumeItemService.updateConsumeProductStatus(id, status).success(function () {
                        methods.getConsumeProducts();
                    });
                })
            },
            updateUserComsumeHisStatus: function (id, status, ship_to, ship_phone, user_address) {
                var modal = utils.confirm({ msg: "收货地址为：" + ship_to + "  " + ship_phone + "  " + user_address + "；是否将该记录设为已发货状态", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    consumeItemService.updateUserComsumeHisStatus(id, status).success(function () {
                        methods.getUserComsumeHiss();
                    });
                })
            },
            consumeProduct_page: function (page) {
                $scope.query.page = page;
                methods.getConsumeProducts('page', $scope.query.page);
            },
            consumeProduct_list: function () {
                $state.go('app.consumeItem.getConsumeProducts');
            },
            consumeItem_list: function () {
                $state.go('app.consumeItem.list');
            }


        }

        angular.extend($scope, methods);
        methods.get_token_setting();
        if ($state.includes('app.consumeItem.detail')) {
            methods.edit();
        } else if ($state.includes('app.consumeItem.userComsumeHis')) {
            methods.getUserComsumeHiss();
        } else if ($state.includes('app.consumeItem.consumeProductDetail')) {
            methods.editConsumeProduct();
        } else if ($state.includes('app.consumeItem.getConsumeProducts')) {
            methods.getConsumeProducts();
        } else {
            methods.search();
        }
    });
});
